package com.test2015.ui.texture;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Container;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.TexturePaint;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TextureTest extends JFrame{
	
	public TextureTest(){
		JPanel jPanel = new MyPanel();
		jPanel.setLayout(null);
		Container contentPane = this.getContentPane();
		contentPane.add(jPanel);
		
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(400, 300);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	public static void main(String[] args) {
		new TextureTest();
	}
	
}

class MyPanel extends JPanel{
	Color c1 = Color.blue;
	Color c2 = Color.YELLOW;
	
	@Override
	protected void paintComponent(Graphics g) {
		int w = this.getWidth(); 
		int h = this.getHeight();
		Graphics2D g2d = (Graphics2D)g;
		setAntiAliasing(g2d, true);
		
		try {
			BufferedImage back = ImageIO.read(this.getClass().getResourceAsStream("back.png"));
			Rectangle2D tr = new Rectangle2D.Double(0, 0, back.getWidth(), back.getHeight());
			TexturePaint tp = new TexturePaint(back, tr);
			g2d.setPaint(tp);
			g2d.fillRect(0, 0, w,h);;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int x = 50;
		int y = 50;
		int diameter1 = 100;
		int diameter2 = diameter1;
		Paint paint1 = getPaint(c1, c2, x+diameter1, 0, x-diameter1, 0);
		Paint paint2 = getPaint(c1, c2, x-diameter1, 0,x + diameter1, 0);
		
		
		Stroke s = getStroke(5);
		g2d.setStroke(s);
		
		g2d.setPaint(paint1);
		g2d.drawArc(x, y, diameter1, diameter2, 0, 180);
		g2d.setPaint(paint2);
		g2d.drawArc(x, y, diameter1, diameter2, 180, 167);
		
		
		int x2 = 10;
		int y2 = 200;
		int w2 = 200;
		int h2 = 16;
		int diameter = 16;
		Paint paint3 = getPaint(c1, c2, x2, 0,x2+w2, 0);
		g2d.setPaint(paint2);
		g2d.fillRoundRect(x2, y2, w2, h2, diameter, diameter);
		
		
//		g2d.drawLine(10, 10, w, h);
	}
	
	
	public static AlphaComposite getSrcOverAC( float alpha){
		AlphaComposite newComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
		return newComposite;
	}
	
	public static Paint getPaint(Color c1,Color c2,int x1,int y1,int x2,int y2){
		GradientPaint paint1 = new GradientPaint(x1, y1, c1, x2, y2, c2);
		return paint1;
	}
	
	public static Stroke getStroke(int width){
		BasicStroke bs1 = new BasicStroke(width, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
		return bs1;
	}
	
	public static void setAntiAliasing(Graphics2D g2 ,boolean antiAliasing)
	{
		if(antiAliasing)
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING
					, RenderingHints.VALUE_ANTIALIAS_ON);
		else
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING
					, RenderingHints.VALUE_ANTIALIAS_OFF);
	}
}







